import Vue from 'vue';
import VueRouter from 'vue-router';
// eslint-disable-next-line import/extensions
import Login from '../views/Login';
import Home from '../views/front/Home'
import Admin from '../views/admin/Admin'
import Welcome from "../components/Welcome";
import UserList from "../views/admin/UserList";
import TabList from "../views/admin/TabList"
import UserAList from '@/views/admin/UserAList';
import ShopList from '@/views/admin/ShopList'
import ShopXQ from '@/views/front/ShopXQ';
import OrderList from '@/views/admin/OrderList';
import CollectList from '@/views/admin/CollectList';
import ShopYList from '@/views/admin/ShopYList';
import CollectYList from '@/views/admin/CollectYList';
import OrderYSList from '@/views/admin/OrderYSList';
import OrderYList from '@/views/admin/OrderYList';
import Collect from '@/views/front/Collect';
import YiShip from '@/views/front/YiShip';
import ChuShip from '@/views/front/ChuShip';
import Register from '../views/Register';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录',
    },
  },
  {
    path: '/register',
    component: Register,
    meta: {
      title: '注册',
    },
  },
  {
    path: '/front/Home',
    component: Home,
    meta: {
      title: '首页',
    },
  },
  {
    path: '/front/collect',
    component: Collect,
    meta: {
      title: '购物车',
    },
  },
  {
    path: '/front/ShopXQ',
    component: ShopXQ,
    name: 'ShopXQ',
    meta: {
      title: '详情',
    },
  },
  {
    path: '/front/Yiship',
    component: YiShip,
    name: 'YiShip',
    meta: {
      title: '已买的商品',
    },
  },
  {
    path: '/front/ChuShip',
    component: ChuShip,
    name: 'ChuShip',
    meta: {
      title: '出售的商品',
    },
  },
  {
    path: '/admin/Admin',
    component: Admin,
    meta: {
      title: '后端首页',
    },
    redirect: "/welcome",
    children:[
      {
        path: '/welcome',
        component:Welcome,
        meta:{
          title: '欢迎'
        }
      },
      {
        path: '/userList',
        component:UserList,
        meta:{
          title: '用户列表'
        }
      },
      {
        path: '/tabList',
        component:TabList,
        meta:{
          title: '导航列表'
        }
      },
      {
        path: '/userAList',
        component:UserAList,
        meta:{
          title: '用户列表'
        }
      },
      {
        path: '/shopList',
        component:ShopList,
        meta:{
          title: '商品列表'
        }
      },
      {
        path: '/orderList',
        component:OrderList,
        meta:{
          title: '订单列表'
        }
      },
      {
        path: '/collectList',
        component:CollectList,
        meta:{
          title: '购物车列表'
        }
      },
      {
        path: '/shopYList',
        component:ShopYList,
        meta:{
          title: '商品列表'
        }
      },
      {
        path: '/collectYList',
        component:CollectYList,
        meta:{
          title: '购物车列表'
        }
      },
      {
        path: '/orderYSList',
        component:OrderYSList,
        meta:{
          title: '出售商品订单列表'
        }
      },
      {
        path: '/orderYList',
        component:OrderYList,
        meta:{
          title: '购买商品订单列表'
        }
      },
    ]
  },
];

const router = new VueRouter({
  routes,
});

//出问题时使用
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location,onResolve,onReject){
  if (onResolve || onReject) return originalPush.call(this,location,onResolve,onReject)
  return originalPush.call(this,location).catch(err => err)
}

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to将要访问
  //from从哪访问
  //next接着干next(url) 重定向到url上 next() 继续访问 to路径
  if (to.path == '/login') return next();
  if (to.path == '/register') return next();
  //获取user
  const userFlag = window.sessionStorage.getItem("user"); //取出user
  if (!userFlag) return next('/login'); //无值，返回登陆
  next(); //符合要求 放行
})

export default router;
